//
//  XHWResetPwdCtrl.m
//  SelfDriving
//
//  Created by q on 2017/10/19.
//  Copyright © 2017年 apple. All rights reserved.
//
#define W ([[UIScreen mainScreen] bounds].size.width)
#define screen_h ([[UIScreen mainScreen] bounds].size.height - (44.0 + [UIApplication sharedApplication].statusBarFrame.size.height))
#import "XHWResetPwdCtrl.h"
#import "UIChooseTableView.h"
#import "BusinessService+User.h"

@interface XHWResetPwdCtrl ()<UIChooseTableViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UILabel *getCodeLbl;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet UITextField *repeatPwdField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UILabel *pwdLbl;

@property (weak, nonatomic) IBOutlet UILabel *repeatPwdLbl;
@property (weak, nonatomic) IBOutlet UILabel *phoneLbl;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UILabel *quhaoLbl;
@property (weak, nonatomic) IBOutlet UILabel *quhaoShowLbl;
@property (weak, nonatomic) IBOutlet UIButton *quhaoBtn;



@end

@implementation XHWResetPwdCtrl
{
    UIChooseTableView *chooseTabel;
    NSString *quhao;
    NSMutableArray *hmdata;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav];
    [self initUI];
    [self getQuhaoData];
}

#pragma mark 请求区号数据
- (void)getQuhaoData {
    hmdata = [[NSMutableArray alloc]init];
    [[BusinessService shareInstance]haoma:[[NSMutableDictionary alloc]init] success:^(id result) {
        hmdata = result;
        _quhaoShowLbl.text = [NSString stringWithFormat:@"+%@",[[hmdata firstObject] objectForKey:@"code"]];
        NSMutableArray *data = [[NSMutableArray alloc]init];
        for (int i = 0; i<hmdata.count; i++) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setObject:[[hmdata objectAtIndex:i] objectForKey:@"code"] forKey:@"id"];
            [dic setObject:[[hmdata objectAtIndex:i] objectForKey:@"name"] forKey:@"name"];
            [data addObject:dic];
        }
        hmdata = data;
    } failure:^(id result) {
    }];
}


#pragma mark 布局
- (void)initNav {
    self.title = @"找回密码";
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_p.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)initUI {
    
    self.navigationController.navigationBar.translucent=NO;
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat margin = 8;
    CGFloat margin_h = 50;
    CGFloat lbl_h = 1;
    _quhaoLbl.frame = CGRectMake(margin, margin_h, 100, lbl_h);
    _quhaoShowLbl.frame = CGRectMake(10, 15, 50, 30);
    _quhaoBtn.frame = CGRectMake(94, 15, 14, 30);
    _phoneLbl.frame = CGRectMake(113, margin_h, W-113-margin, lbl_h);
    _phoneField.frame = CGRectMake(113, 10, _phoneLbl.frame.size.width, 40);
    _phoneField.keyboardType = UIKeyboardTypePhonePad;
    _phoneField.delegate = self;
    _pwdLbl.frame = CGRectMake(margin, margin_h*2+lbl_h, W-margin*2, lbl_h);
    _pwdField.frame = CGRectMake(margin, 61, _pwdLbl.width, _phoneField.height);
    _repeatPwdLbl.frame = CGRectMake(margin, margin_h*3+lbl_h*2, _pwdLbl.width, lbl_h);
    _repeatPwdField.frame = CGRectMake(margin, (margin_h+lbl_h)*2+10, _pwdLbl.width, _phoneField.height);
    _getCodeBtn.frame = CGRectMake(margin, (margin_h+lbl_h)*3+10, _quhaoLbl.width, 40);
    _getCodeLbl.frame = CGRectMake(113, (margin_h+lbl_h)*3+50, _phoneLbl.width, lbl_h);
    _codeField.frame = CGRectMake(113, (margin_h+lbl_h)*3+10, _phoneLbl.width, _phoneField.height);
    _submitBtn.frame = CGRectMake(0, screen_h-40, W, 40);
}
- (IBAction)submitBtnAction:(UIButton *)sender {

    
    if([_phoneField.text trim].length == 0 ){
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    else {
        if (![ValidateHelper isValidateMobile:_phoneField.text]) {
            [SVProgressHUD showErrorWithStatus:@"手机号格式不正确"];
            return;
        }
    }
    if([_codeField.text trim].length == 0 ){
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    if([_pwdField.text trim].length == 0 ){
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    if([_repeatPwdField.text trim].length == 0 ){
        [SVProgressHUD showErrorWithStatus:@"请再次确认密码"];
        return;
    }
    if ([_phoneField.text isEqualToString:_repeatPwdField.text]) {
        [SVProgressHUD showErrorWithStatus:@"两次输入的密码不一致"];
        return;
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[_pwdField.text trim] forKey:@"newpassword"];
    [dic setObject:[_phoneField.text trim] forKey:@"phone"];
    [dic setObject:[_codeField.text trim] forKey:@"code"];
    [[BusinessService shareInstance] findPwd:dic success:^(id result) {
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        NSLog(@"修改成功");
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:@"修改失败"];
    }];
}
- (IBAction)codeBtnAction:(UIButton *)sender {
    
    if([_phoneField.text trim].length == 0 ){
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    else {
        if (![ValidateHelper isValidateMobile:_phoneField.text]) {
            [SVProgressHUD showErrorWithStatus:@"手机号格式不正确"];
            return;
        }
    }
    NSMutableDictionary *dic= [[NSMutableDictionary alloc]init];
    [dic setObject:[_phoneField.text trim] forKey:@"phone"];
    [dic setObject:@"1" forKey:@"type"];
    
    [[BusinessService shareInstance]getCode:dic success:^(id result) {
        [SVProgressHUD showSuccessWithStatus:@"获取验证码成功"];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}

- (IBAction)quhaoxuanzeBtnAction:(UIButton *)sender {
    
    chooseTabel = [[UIChooseTableView alloc] initWithFrame:CGRectMake(0.0f, SCRE_HEIGHT, SCRE_WIDTH, 260.0f)];
    chooseTabel.delegate = self;
    chooseTabel.lanel_title.text = @"选择区号";
    chooseTabel.mainData  = hmdata;
    [chooseTabel up];
    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:chooseTabel];
}
#pragma mark UIChooseTableView回调方法
-(void)didSelectDate:(UITableView *)chooseTableView indexPath:(NSIndexPath *)indexPath string:(NSMutableDictionary *)string{
    _quhaoShowLbl.text  = [NSString stringWithFormat:@"+%@",[string objectForKey:@"id"]];
    quhao = [string objectForKey:@"id"];
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([textField isEqual:_phoneField]) {
        if ([string length] != 0) {
            UniChar single = [string characterAtIndex:0];
            if (single <= '9' && single >= '0') {
                return YES;
            }
            return NO;
        }
        return YES;
    }
    return YES;
}




@end
